package com.sojson.controller.po.param.verify;

import java.sql.SQLException;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.entity.bo.AuthcUser;
import com.sojson.entity.po.Member;
import com.sojson.entity.vo.MemberVo;
import com.sojson.result.Result;
import com.sojson.util.MatcheUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.token.TokenUtil;
import com.sojson.util.token.TokenVo;
import com.sojson.util.verifycode.VerifyCodeUtil;

/**
 * 用户表控制器参数验证
 * 
 * @author liu
 * @date 2021-03-16
 */
public class MemberParamVerify {

    /**
     * 通过id更新(局部更新)
     * 
     * @param member
     * @return
     */
    public static Result<?> updatePartialById(Member member) {
        if (StringUtil.isBlank(member.getId())) {
            return Result.fail("ID不能为空！");
        }
        if (StringUtil.isBlankObject(member.getSelfAppraisal())) {
            member.setSelfAppraisal("");
        }
        String mobile = member.getMobile();
        if (StringUtil.isNotBlankObject(mobile) && !MatcheUtil.isMobile(mobile)) {
            return Result.fail("手机号格式错误！");
        }
        Float targetX = member.getTargetX();
        if (StringUtil.isNotBlank(targetX)) {
            if (targetX > 180 || targetX <= -180) {
                return Result.fail("经度值错误！");
            }
        }
        Float targetY = member.getTargetY();
        if (StringUtil.isNotBlank(targetY)) {
            if (targetY > 90 || targetY < -90) {
                return Result.fail("纬度值错误！");
            }
        }

        return Result.success();
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param member
     * @return
     */
    public static Result<?> updateAuth(Member member) {
        if (StringUtil.isBlank(member.getId())) {
            return Result.fail("ID不能为空！");
        }
        if (StringUtil.isBlank(member.getName())) {
            return Result.fail("姓名不能为空！");
        }
        if (StringUtil.isBlank(member.getIdCard())) {
            return Result.fail("身份证号不能为空！");
        }
        return Result.success();
    }

    /**
     * 通过id更新手机号
     * 
     * @param token
     * @return
     * @throws Exception 
     * @throws SQLException
     */
    public static Result<?> updateMobileById(TokenVo token) throws Exception {
        if (StringUtil.isBlank(token.getVerifyCodeValue())) {
            return Result.fail("验证码不能为空！");
        }
        Member member = token.getMember();
        if (StringUtil.isBlank(member)) {
            return Result.fail("member参数不能为空！");
        }
        String mobile = member.getMobile();

        if (StringUtil.isBlank(member.getId())) {
            return Result.fail("ID不能为空！");
        }
        if (StringUtil.isBlank(mobile)) {
            return Result.fail("手机号不能为空！");
        }
        if (!MatcheUtil.isMobile(mobile)) {
            return Result.fail("手机号格式错误！");
        }

        return Result.success();
    }

    /**
     * 通过id验证旧密码修改密码
     * 
     * @param member
     * @return
     * @throws Exception 
     */
    public static Result<?> updatePwdById(AuthcUser member) {
        if (StringUtil.isBlank(member.getId())) {
            return Result.fail("ID不能为空！");
        }
        if (StringUtil.isBlank(member.getPwd())) {
            return Result.fail("密码不能为空！");
        }

        return Result.success();
    }

    /**
     * 通过id验证旧密码修改密码
     * 
     * @param member
     * @return
     * @throws Exception 
     */
    public static Result<?> updatePwdByOldPwd(AuthcUser member) {
        if (StringUtil.isBlank(member.getOldPwd())) {
            return Result.fail("旧密码不能为空！");
        }
        if (StringUtil.isBlank(member.getPwd())) {
            return Result.fail("密码不能为空！");
        }

        return Result.success();
    }

    /**
     * 通过id验证短信修改密码
     * 
     * @param token
     * @return
     * @throws Exception 
     */
    public static Result<?> updatePwdBySms(TokenVo token) throws Exception {
        String verifyCodeValue = token.getVerifyCodeValue();
        if (StringUtil.isBlank(verifyCodeValue)) {
            return Result.fail("验证码不能为空！");
        }
        Member member = token.getMember();
        if (StringUtil.isBlank(member)) {
            return Result.fail("member参数不能为空！");
        }
        if (StringUtil.isBlank(member.getId())) {
            return Result.fail("ID不能为空！");
        }
        if (StringUtil.isBlank(member.getPwd())) {
            return Result.fail("密码不能为空！");
        }
        String id = TokenUtil.getClaims().getId();
        if (!VerifyCodeUtil.verifyAndClearCode(
            new StringBuilder(id).append(VerifyCodeUtil.SMS).append(token.getVerifyCodePrefix()).toString(),
            verifyCodeValue)) {
            return Result.fail(TransErrorCode.ERR_CODES.getMsg());
        }

        return Result.success();
    }

    /**
     * 通过id修改状态
     * 
     * @param member
     * @return
     * @throws Exception 
     */
    public static Result<?> updateStatusById(Member member) {
        if (StringUtil.isBlank(member.getId())) {
            return Result.fail("ID不能为空！");
        }
        if (StringUtil.isBlank(member.getStatus())) {
            return Result.fail("状态不能为空！");
        }

        return Result.success();
    }

    /**
     * 通过id修改认证状态
     * 
     * @param member
     * @return
     * @throws Exception 
     */
    public static Result<?> auditAuth(Member member) {
        if (StringUtil.isBlank(member.getId())) {
            return Result.fail("ID不能为空！");
        }
        if (StringUtil.isBlank(member.getAuthStatus())) {
            return Result.fail("认证状态不能为空！");
        }

        return Result.success();
    }

    /**
     * 修改支付密码
     * 
     * @param member
     * @return
     */
    public static Result<?> updatePayPwd(AuthcUser member) {
        if (StringUtil.isBlank(member.getPayPwd())) {
            return Result.fail("支付密码不能为空！");
        }

        return Result.success();
    }

    /**
     * 修改支付密码
     * 
     * @param member
     * @return
     */
    public static Result<?> updatePayPwdWeb(AuthcUser member) {
        if (StringUtil.isBlank(member.getId())) {
            return Result.fail("ID不能为空！");
        }
        if (StringUtil.isBlank(member.getPayPwd())) {
            return Result.fail("支付密码不能为空！");
        }

        return Result.success();
    }

    /**
     * 用户续租
     * 
     * @param member
     * @return
     */
    public static Result<?> addDayGoOn(MemberVo member) {
        if (StringUtil.isBlank(member.getNo())) {
            return Result.fail("编号不能为空！");
        }
        Integer dayGoOn = member.getDayGoOn();
        if (StringUtil.isBlankObject(dayGoOn)) {
            return Result.fail("续租天数不能为空！");
        }
        if (dayGoOn < 1) {
            return Result.fail("续租天数不能为空！");
        }

        return Result.success();
    }

}